package com.yanqu.road.server.cmd.cookboy;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_COOK_BOY_HISTORY_MATCH, desc = "查看历史")
public class CookBoyHistoryMatchCmd extends AbstractCommand {

	int activityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CookBoyProto.CookBoyHistoryMatchReqMsg reqMsg = CookBoyProto.CookBoyHistoryMatchReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = CookBoyActivityMgr.getInTimeActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!CookBoyActivityMgr.isActivityMatchTime(activityInfo)){
			ret = GameErrorCode.E_COOK_BOY_NOT_IN_MATCH_TIME;
			return;
		}
		CookBoyProto.CookBoyHistoryMatchReqMsg.Builder reqMsg = CookBoyProto.CookBoyHistoryMatchReqMsg.newBuilder();
		reqMsg.setActivityId(activityId);
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_COOK_BOY_MATCH_HISTORY, reqMsg);
		GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			CookBoyProto.CookBoyMakeFoodRespMsg.Builder respMsg = CookBoyProto.CookBoyMakeFoodRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_COOK_BOY_HISTORY_MATCH, respMsg);
		}
	}
}
